package org.dong.samples.service;

import java.util.ArrayList;
import java.util.List;
import org.dong.samples.dao.TeacherHibernateDAO;
import org.dong.samples.model.User;
import org.dong.samples.model.UserSubject;
import org.dong.samples.model.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
	
@Service
public class TeacherSerivce {
	
	@Autowired
	private TeacherHibernateDAO teacherHibernateDAO;
	
	@Autowired
	private UserSubjectHibernateService userSubjectHibernateService;
	
	public void saveSubjectStudent(List<String> list, Integer subId) {
		List<String> dbSubjectUserList = teacherHibernateDAO.getAllUserBySubject(subId);
		List<String> insertList = new ArrayList<String>();
		List<String> delList = new ArrayList<String>();
		
		for(String selectedUser:list){
			if(!dbSubjectUserList.contains(selectedUser)){
				insertList.add(selectedUser);
			}
		}
		
		for(String selectedUser:dbSubjectUserList){
			if(!list.contains(selectedUser)){
				delList.add(selectedUser);
			}
		}
		
		teacherHibernateDAO.insetSubjectUsers(insertList, subId);
		teacherHibernateDAO.deleteSubjectUsers(delList, subId);
	}

	public List<UserVO> getAllStudent() throws Exception {
		List<User> userList = userSubjectHibernateService.findAllUser();
		List<UserVO> resultList = new ArrayList<UserVO>();
		for(User user:userList){
			UserVO vo = new UserVO();
			BeanUtils.copyProperties(user, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	public List<String> getAllUserBySubject(Integer subId) {
		return teacherHibernateDAO.getAllUserBySubject(subId);
	}
	
	public List<UserSubject> getUserBySubjectId(Integer subId){
		return teacherHibernateDAO.getUserBySubjectId(subId);
	}
	
}
